package org.farng.mp3.id3;

import org.farng.mp3.AbstractMP3Fragment;
import org.farng.mp3.InvalidTagException;
import org.farng.mp3.TagUtility;

import java.io.IOException;
import java.io.RandomAccessFile;


/**
 * Title:       ID3v2Frame Description: This abstract class is each frame
 * header inside a ID3v2 tag Copyright:   Copyright (c) 2002 Company:
 *
 * @author Eric Farng
 * @version $Revision: 1.3 $
 */
public abstract class AbstractID3v2Frame extends AbstractMP3Fragment {
    public AbstractID3v2Frame() {
        super();
    }

    public AbstractID3v2Frame(AbstractID3v2Frame body) {
        super(body);
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String getIdentifier() {
        if (body == null) {
            return "";
        }

        return body.getIdentifier();
    }

    /**
     * DOCUMENT ME!
     *
     * @param identifier DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public boolean isValidID3v2FrameIdentifier(String identifier) {
        char    ch;
        boolean valid = true;

        for (int i = 0; i < identifier.length(); i++) {
            ch = identifier.charAt(i);

            if (((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9'))) {
                // nothing
            } else {
                valid = false;
            }
        }

        return valid;
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public String toString() {
        if (body == null) {
            return "";
        }

        return body.toString();
    }

    /**
     * DOCUMENT ME!
     *
     * @param identifier DOCUMENT ME!
     * @param file DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws IOException DOCUMENT ME!
     * @throws InvalidTagException DOCUMENT ME!
     */
    protected AbstractID3v2FrameBody readBody(String identifier, RandomAccessFile file)
                                       throws IOException, InvalidTagException {
        String                 id = "";
        AbstractID3v2FrameBody newBody = null;

        if (TagUtility.isID3v2_2FrameIdentifier(identifier)) {
            id = (String) TagUtility.convertFrameID2_2to2_4(identifier);
        } else {
            id = identifier;
        }

        if (id.equals("APIC")) {
            newBody = new FrameBodyAPIC(file);
        } else if (id.equals("COMM")) {
            newBody = new FrameBodyCOMM(file);
        } else if (id.equals("ENCR")) {
            newBody = new FrameBodyENCR(file);
        } else if (id.equals("GEOB")) {
            newBody = new FrameBodyGEOB(file);
        } else if (id.equals("GRID")) {
            newBody = new FrameBodyGRID(file);
        } else if (id.equals("MCDI")) {
            newBody = new FrameBodyMCDI(file);
        } else if (id.equals("PCNT")) {
            newBody = new FrameBodyPCNT(file);
        } else if (id.equals("POPM")) {
            newBody = new FrameBodyPOPM(file);
        } else if (id.equals("PRIV")) {
            newBody = new FrameBodyPRIV(file);
        } else if (id.equals("RVAD")) {
            newBody = new FrameBodyRVAD(file);
        } else if (id.equals("SYLT")) {
            newBody = new FrameBodySYLT(file);
        } else if (id.equals("TALB")) {
            newBody = new FrameBodyTALB(file);
        } else if (id.equals("TBPM")) {
            newBody = new FrameBodyTBPM(file);
        } else if (id.equals("TCOM")) {
            newBody = new FrameBodyTCOM(file);
        } else if (id.equals("TCON")) {
            newBody = new FrameBodyTCON(file);
        } else if (id.equals("TCOP")) {
            newBody = new FrameBodyTCOP(file);
        } else if (id.equals("TDAT")) {
            newBody = new FrameBodyTDAT(file); // Deprecated
        } else if (id.equals("TDEN")) {
            newBody = new FrameBodyTDEN(file);
        } else if (id.equals("TDLY")) {
            newBody = new FrameBodyTDLY(file);
        } else if (id.equals("TDOR")) {
            newBody = new FrameBodyTDOR(file);
        } else if (id.equals("TDRC")) {
            newBody = new FrameBodyTDRC(file);
        } else if (id.equals("TDRL")) {
            newBody = new FrameBodyTDRL(file);
        } else if (id.equals("TDTG")) {
            newBody = new FrameBodyTDTG(file);
        } else if (id.equals("TENC")) {
            newBody = new FrameBodyTENC(file);
        } else if (id.equals("TEXT")) {
            newBody = new FrameBodyTEXT(file);
        } else if (id.equals("TFLT")) {
            newBody = new FrameBodyTFLT(file);
        } else if (id.equals("TIME")) {
            newBody = new FrameBodyTIME(file); // Deprecated
        } else if (id.equals("TIPL")) {
            newBody = new FrameBodyTIPL(file);
        } else if (id.equals("TIT1")) {
            newBody = new FrameBodyTIT1(file);
        } else if (id.equals("TIT2")) {
            newBody = new FrameBodyTIT2(file);
        } else if (id.equals("TIT3")) {
            newBody = new FrameBodyTIT3(file);
        } else if (id.equals("TKEY")) {
            newBody = new FrameBodyTKEY(file);
        } else if (id.equals("TLAN")) {
            newBody = new FrameBodyTLAN(file);
        } else if (id.equals("TLEN")) {
            newBody = new FrameBodyTLEN(file);
        } else if (id.equals("TMCL")) {
            newBody = new FrameBodyTMCL(file);
        } else if (id.equals("TMED")) {
            newBody = new FrameBodyTMED(file);
        } else if (id.equals("TMOO")) {
            newBody = new FrameBodyTMOO(file);
        } else if (id.equals("TOAL")) {
            newBody = new FrameBodyTOAL(file);
        } else if (id.equals("TOFN")) {
            newBody = new FrameBodyTOFN(file);
        } else if (id.equals("TOLY")) {
            newBody = new FrameBodyTOLY(file);
        } else if (id.equals("TOPE")) {
            newBody = new FrameBodyTOPE(file);
        } else if (id.equals("TORY")) {
            newBody = new FrameBodyTORY(file); // Deprecated
        } else if (id.equals("TOWN")) {
            newBody = new FrameBodyTOWN(file);
        } else if (id.equals("TPE1")) {
            newBody = new FrameBodyTPE1(file);
        } else if (id.equals("TPE2")) {
            newBody = new FrameBodyTPE2(file);
        } else if (id.equals("TPE3")) {
            newBody = new FrameBodyTPE3(file);
        } else if (id.equals("TPE4")) {
            newBody = new FrameBodyTPE4(file);
        } else if (id.equals("TPOS")) {
            newBody = new FrameBodyTPOS(file);
        } else if (id.equals("TPRO")) {
            newBody = new FrameBodyTPRO(file);
        } else if (id.equals("TPUB")) {
            newBody = new FrameBodyTPUB(file);
        } else if (id.equals("TRCK")) {
            newBody = new FrameBodyTRCK(file);
        } else if (id.equals("TRDA")) {
            newBody = new FrameBodyTRDA(file); // Deprecated
        } else if (id.equals("TRSN")) {
            newBody = new FrameBodyTRSN(file);
        } else if (id.equals("TRSO")) {
            newBody = new FrameBodyTRSO(file);
        } else if (id.equals("TSIZ")) {
            newBody = new FrameBodyTSIZ(file); // Deprecated
        } else if (id.equals("TSOA")) {
            newBody = new FrameBodyTSOA(file);
        } else if (id.equals("TSOP")) {
            newBody = new FrameBodyTSOP(file);
        } else if (id.equals("TSOT")) {
            newBody = new FrameBodyTSOT(file);
        } else if (id.equals("TSRC")) {
            newBody = new FrameBodyTSRC(file);
        } else if (id.equals("TSSE")) {
            newBody = new FrameBodyTSSE(file);
        } else if (id.equals("TSST")) {
            newBody = new FrameBodyTSST(file);
        } else if (id.equals("TXXX")) {
            newBody = new FrameBodyTXXX(file);
        } else if (id.equals("TYER")) {
            newBody = new FrameBodyTYER(file); // Deprecated
        } else if (id.equals("UFID")) {
            newBody = new FrameBodyUFID(file);
        } else if (id.equals("USLT")) {
            newBody = new FrameBodyUSLT(file);
        } else if (id.equals("WCOM")) {
            newBody = new FrameBodyWCOM(file);
        } else if (id.equals("WCOP")) {
            newBody = new FrameBodyWCOP(file);
        } else if (id.equals("WOAF")) {
            newBody = new FrameBodyWOAF(file);
        } else if (id.equals("WOAR")) {
            newBody = new FrameBodyWOAR(file);
        } else if (id.equals("WOAS")) {
            newBody = new FrameBodyWOAS(file);
        } else if (id.equals("WORS")) {
            newBody = new FrameBodyWORS(file);
        } else if (id.equals("WPAY")) {
            newBody = new FrameBodyWPAY(file);
        } else if (id.equals("WPUB")) {
            newBody = new FrameBodyWPUB(file);
        } else if (id.equals("WXXX")) {
            newBody = new FrameBodyWXXX(file);
        } else {
            newBody = new FrameBodyUnsupported(file);
        }

        return newBody;
    }
}